#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
查看采集到的问题 - 正确显示UTF-8编码
"""

import requests
import json
import sys

def view_questions():
    """查看问题列表"""
    try:
        response = requests.get("http://localhost:5000/api/questions")
        if response.status_code == 200:
            # 获取原始JSON文本
            data = response.json()
            questions = data.get("questions", [])
            total = data.get("total", 0)
            
            print("🎯 采集到的问题列表")
            print("=" * 60)
            print(f"总计: {total} 个问题")
            print("=" * 60)
            
            if questions:
                for i, question in enumerate(questions, 1):
                    print(f"{i}. 用户: {question['user']}")
                    print(f"   问题: {question['question']}")
                    print(f"   时间: {question['timestamp']}")
                    print(f"   房间: {question['room_id']}")
                    print("-" * 40)
            else:
                print("暂无问题")
            
            # 显示原始JSON (证明UTF-8编码工作正常)
            print("\n📋 原始JSON响应:")
            print(json.dumps(data, ensure_ascii=False, indent=2))
            
        else:
            print(f"❌ 获取问题失败: HTTP {response.status_code}")
    
    except Exception as e:
        print(f"❌ 错误: {e}")

def test_utf8_encoding():
    """测试UTF-8编码"""
    print("🧪 UTF-8编码测试")
    print("=" * 60)
    
    # 测试中文字符
    test_strings = [
        "人工测试？？",
        "能用了？",
        "可以了是吧？？",
        "解决了吗？？",
        "测试问题？？"
    ]
    
    for i, text in enumerate(test_strings, 1):
        print(f"{i}. 原始字符串: {text}")
        print(f"   UTF-8编码: {text.encode('utf-8')}")
        print(f"   Unicode编码: {repr(text)}")
        print()

if __name__ == "__main__":
    print("🎯 问题查看器 - UTF-8编码测试")
    print("=" * 60)
    
    # 测试UTF-8编码
    test_utf8_encoding()
    
    # 查看实际问题
    view_questions()